package com.gugee.ins.common.dal.service.media.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.media.InsRelMediaTaggerUserMapper;
import com.gugee.ins.common.dal.service.media.InsRelMediaTaggerUserDao;
import com.gugee.ins.common.model.es.blogger.InsBloggerTaggedBrand;
import com.gugee.ins.common.model.es.media.InsMediaTaggedBlogger;
import com.gugee.ins.common.model.mysql.media.InsRelMediaTaggerUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * insBlogger dao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsRelMediaTaggerUserDaoImpl extends ServiceImpl<InsRelMediaTaggerUserMapper, InsRelMediaTaggerUser>
        implements InsRelMediaTaggerUserDao {
    /**
     * 批量插入
     * @param insRelMediaTaggerUsers
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<InsRelMediaTaggerUser> insRelMediaTaggerUsers) {
        try{
            this.saveBatch(insRelMediaTaggerUsers);
        }catch (Exception e){
            log.debug("batch insert ins rel media tagger user failed ,try insert one by one, msg : {} ",e.getMessage());
            insRelMediaTaggerUsers.forEach(insRelMediaTaggerUser -> {
                try{
                    baseMapper.insert(insRelMediaTaggerUser);
                }catch (Exception ex){
                    log.info("insert ins rel media tagger user failed ,uid : {} ,shortcode : {},taggerUid : {}, msg : {} "
                            ,insRelMediaTaggerUser.getUid(),insRelMediaTaggerUser.getShortcode()
                            ,insRelMediaTaggerUser.getTaggerUid(),ex.getMessage());
                }
            });
        }
    }

    @Override
    public List<InsBloggerTaggedBrand> getTaggedBrandByUid(long uid) {
        return baseMapper.findTaggedBrandByUid(uid);
    }

    @Override
    public List<InsMediaTaggedBlogger> getInsMediaTaggedBloggerByShotCode(String shortCode) {
        return baseMapper.getInsMediaTaggedBloggerByShotCode(shortCode);
    }
}
